package com.lpdemilis.pokermanager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class CreateGroupActivity extends Activity implements MyAsyncListener {
	private User user;
	private int groupindex;
	private int groupid;
	protected String SERVER_URL;
	protected CustomHttpClient client;

	protected TextView returnTextView;
	protected EditText groupFullnameEditText;
	protected EditText groupShortnameEditText;
	protected EditText enrolPasswordEditText;
	protected EditText groupDescriptionEditText;
	protected Button createGroupButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_creategroup);

		if (Build.MANUFACTURER.equals("unknown")) {
			SERVER_URL = this.getString(R.string.URL_local);
		} else {
			SERVER_URL = this.getString(R.string.URL);
		}
		
		TextView activityTitleTextView = (TextView) findViewById(R.id.activityTitleTextView);
		Typeface titleTypeface = Typeface.createFromAsset(getAssets(),
                "fonts/POKERPN.TTF");        
        activityTitleTextView.setTypeface(titleTypeface);

		user = (User) getIntent().getSerializableExtra("user");
		groupindex = getIntent().getIntExtra("groupindex", 0);
		groupid = 0;

		// interface
		returnTextView = (TextView) findViewById(R.id.returnTextView);
		groupFullnameEditText = (EditText) findViewById(R.id.groupFullnameEditText);
		groupShortnameEditText = (EditText) findViewById(R.id.groupShortnameEditText);
		enrolPasswordEditText = (EditText) findViewById(R.id.enrolPasswordEditText);
		groupDescriptionEditText = (EditText) findViewById(R.id.groupDescriptionEditText);
		createGroupButton = (Button) findViewById(R.id.createGroupButton);

		// listener
		createGroupButton.setOnClickListener(createGroupButtonClickListener);
	}

	public enum ActionsEnum {
		createGroup, enrolUser, error;
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(CreateGroupActivity.this, MainActivity.class);
		intent.putExtra("groupindex", groupindex);
		intent.putExtra("user", user);
		if (groupid > 0) {
			intent.putExtra("groupid", groupid);
		}
		CreateGroupActivity.this.startActivity(intent);
		CreateGroupActivity.this.finish();
	}

	View.OnClickListener createGroupButtonClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if (groupFullnameEditText.getText().toString().trim().equals("")) {
				returnTextView.setText(R.string.empty_group_fullname);
				groupFullnameEditText.requestFocus();
				return;
			} else if (groupShortnameEditText.getText().toString().trim()
					.equals("")) {
				returnTextView.setText(R.string.empty_group_shortname);
				groupShortnameEditText.requestFocus();
				return;
			} else {
				returnTextView.setText("");
				createGroup();
			}
		}
	};

	public void createGroup() {
		try {
			client = new CustomHttpClient(this,
					this.getString(R.string.wait_message), true);
			String action = ActionsEnum.createGroup.toString();
			String paramName1 = "userid";
			String paramValue1 = String.valueOf(user.getId());
			String paramName2 = "groupfullname";
			String paramValue2 = groupFullnameEditText.getText().toString();
			String paramName3 = "groupshortname";
			String paramValue3 = groupShortnameEditText.getText().toString();
			String paramName4 = "enrolpassword";
			String paramValue4 = enrolPasswordEditText.getText().toString();
			String paramName5 = "groupdescription";
			String paramValue5 = groupDescriptionEditText.getText().toString();
			client.execute(SERVER_URL, action, paramName1, paramValue1,
					paramName2, paramValue2, paramName3, paramValue3,
					paramName4, paramValue4, paramName5, paramValue5);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void executeReturn(String returnValue) {
		String[] returnArray;
		returnArray = returnValue.split("##");

		ActionsEnum action;

		try {
			action = ActionsEnum.valueOf(returnArray[0]);
		} catch (Exception e) {
			action = ActionsEnum.error;
		}

		switch (action) {
		case createGroup:
			createGroupReturn(returnArray);
			break;
		case error:
			errorReturn(returnArray);
			break;
		}
	}

	private void createGroupReturn(final String[] returnArray) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle(R.string.creategroup);

		if (returnArray.length <= 1) {
			alertDialog.setMessage(this.getString(R.string.create_group_error));
			alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
					this.getString(android.R.string.ok),
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							return;
						}
					});
			alertDialog.show();
		} else {
			if (returnArray[2].equals("fullnametaken")) {
				alertDialog.setMessage(this
						.getString(R.string.fullname_taken_error));
				alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
						this.getString(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								groupFullnameEditText.requestFocus();
							}
						});
				alertDialog.show();
			} else if (returnArray[2].equals("shortnametaken")) {
				alertDialog.setMessage(this
						.getString(R.string.shortname_taken_error));
				alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
						this.getString(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								groupShortnameEditText.requestFocus();
							}
						});
				alertDialog.show();
			} else if (returnArray[1].equals("groupid")) {
				alertDialog.setMessage(this.getString(R.string.group_created));
				alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
						this.getString(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								groupid = Integer.parseInt(returnArray[2]);
								onBackPressed();
							}
						});
				alertDialog.show();
			}
		}
	}

	private void errorReturn(String[] returnArray) {

	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_creategroup, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		Intent intent;
		switch (item.getItemId()) {
		case R.id.menu_logoff:
			intent = new Intent(CreateGroupActivity.this, LoginActivity.class);
			CreateGroupActivity.this.startActivity(intent);
			CreateGroupActivity.this.finish();
			return true;
		case R.id.menu_searchgroup:
			intent = new Intent(CreateGroupActivity.this, SearchGroupActivity.class);
			intent.putExtra("user", user);
			intent.putExtra("groupindex",groupindex);
			intent.putExtra("groupid", groupid);
			CreateGroupActivity.this.startActivity(intent);
			CreateGroupActivity.this.finish();
			return true;		
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
